/*
 * Filename    : Entity_Rama.java
 * Projectname : RamaShinta
 * Date        : Nov 29, 2012 , 2:57:09 PM 
 * 
 */

package com.amikomgamedev.ramashinta.entity;

import java.util.ArrayList;

import org.andengine.entity.modifier.MoveYModifier;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.util.color.Color;

import com.amikomgamedev.ramashinta.Utils;
import com.amikomgamedev.ramashinta.game.Game_Config;
import com.amikomgamedev.ramashinta.game.Game_Data;
import com.amikomgamedev.ramashinta.graphic.Graphic_Anim;
import com.amikomgamedev.ramashinta.graphic.Graphic_Attach;
import com.amikomgamedev.ramashinta.graphic.Graphic_Detach;
import com.amikomgamedev.ramashinta.sound.Sound_Manager;

public class Entity_Rama implements Graphic_Anim, Game_Data
{
	private BaseGameActivity		m_Activity;
	private Graphic_Detach			m_Graphic;

	private int						m_Mc_State	= -1;

	private Rectangle				m_Mc_Rect;
	private float					mc_Pos_Y;

	private final float				MOVE_SPEED	= Utils.getRatio(4f);

	public static AnimatedSprite			m_Mc;
	public static AnimatedSprite			c_Mc;
	public Entity_Panah	listPanahRama[]	= new Entity_Panah[10];
	
	public static int lastPoin;
	public static int poin;
	
	public Bar barRama;
	

	public static boolean panahApiAktif=false;
	public static  boolean timerApi = true;
	

	public static boolean timerArmor = true;
	public static boolean armorAktif = false;
	

	public Entity_Rama(AnimatedSprite p_Mc, AnimatedSprite p2_Mc, BaseGameActivity p_Activity, Graphic_Detach p_Graphic, Sound_Manager pSound)
	{
		poin = 0;
		lastPoin = 0;
		
		m_Mc = p_Mc;
		c_Mc = p2_Mc;
		m_Activity = p_Activity;
		m_Graphic = p_Graphic;
		mc_Pos_Y = m_Mc.getY();
		
		barRama = new Bar(m_Activity, 0, 0, m_Graphic, 1);

		m_Mc_Rect = new Rectangle(0, 0, m_Mc.getWidth(), m_Mc.getHeight(), m_Activity.getVertexBufferObjectManager());
		if (Game_Config.DEBUG)
		{
			m_Mc_Rect.setColor(Color.BLUE);
		} else
		{
			m_Mc_Rect.setColor(Color.TRANSPARENT);
		}

		m_Mc.attachChild(m_Mc_Rect);
		initMc();
		for (int i = 0; i < listPanahRama.length; i++)
		{
			listPanahRama[i] = new Entity_Panah(m_Activity, Utils.getRatio(600), Utils.getRatio(600), m_Graphic, m_Mc, true, Entity_Panah.PANAHNYA_RAMA,pSound);
		}
		
		

		// kosongkan
//		if (!listPanahRama[])
//		{
//			listPanahRama.clear();
//			// for (int i = 0; i < listPanah.size(); i++)
//			// {
//			// listPanah.remove(i);
//			// }
//		}
	}

	private void initMc()
	{
		Utils.TRACE("init mc");
		switch_State(MC_UP);
		m_Mc.setY(RES_HEIGHT - m_Mc.getHeight());
		m_Mc.registerEntityModifier(new MoveYModifier(2f, m_Mc.getY(), (RES_HEIGHT / 2) - m_Mc.getHeight() / 2));
	}

	public void onUpdate(float pSecondElapsed, Entity_Shinta shinta, Entity_Rama rama)
	{
		switch (m_Mc_State)
		{
			case MC_UP:
				if (!checkCollitionRama(MC_UP))
				{
					m_Mc.setY(m_Mc.getY() - MOVE_SPEED);
				}

				break;

			case MC_DOWN:
				if (!checkCollitionRama(MC_DOWN))
				{
					m_Mc.setY(m_Mc.getY() + MOVE_SPEED);
				}
				break;
		}
//
		for (int i = 0; i < listPanahRama.length; i++)
		{
			listPanahRama[i].onUpdate(pSecondElapsed, shinta, Entity_Panah.PANAHNYA_RAMA);
			float currentPanahPos = listPanahRama[i].getPosX();
//			System.out.print("current pos panah "+ currentPanahPos);
			if (listPanahRama[i].getPosX() > RES_WIDTH)
			{
				listPanahRama[i].HilangkanPanahRama();
				Utils.TRACE("Removeeeeeeee");
			}

		}
		
		UpdatePendapatan();

	}
	
	ArrayList<Rectangle> barHati;
	
	void UpdatePendapatan()
	{
		if(lastPoin != poin)
		{
			int perubahan = poin - lastPoin;
			lastPoin = poin;
			
			barRama.spr[1].setY(barRama.spr[1].getY()-(perubahan*Utils.getRatioH(30)));
			float currentBarRamaY = barRama.spr[1].getY();
			
			System.out.print("current pos" + String.valueOf(currentBarRamaY));
			
			
			if(barRama.spr[1].getY() > 0)
			{
				barRama.spr[1].setY(0);
			}
		}
		
	}

	public void switch_State(int p_State)
	{
		m_Mc_State = p_State;
		switch (p_State)
		{
			case MC_IDLE:
				m_Mc.animate(MC_RAMA_DUR[MC_IDLE], MC_RAMA_FRAME[MC_IDLE], true);
				break;
			case MC_UP:
				m_Mc.animate(MC_RAMA_DUR[MC_UP], MC_RAMA_FRAME[MC_UP], 1);

				break;
			case MC_DOWN:
				m_Mc.animate(MC_RAMA_DUR[MC_DOWN], MC_RAMA_FRAME[MC_DOWN], 1);

				break;
			case MC_ATTACK:
				m_Mc.animate(MC_RAMA_DUR[MC_ATTACK], MC_RAMA_FRAME[MC_ATTACK], 1);
				break;
			case MC_CELEBRASI:
				c_Mc.animate(MC_RAMA_DUR[0], MC_RAMA_FRAME[0], true);
				break;

		}
	}

	private boolean checkCollitionRama(int state)
	{

		if (m_Mc_Rect.collidesWith(m_Graphic.rect_Gameplay[0]) && state == MC_UP)
		{
			switch_State(MC_IDLE);
			return true;
		} else if (m_Mc_Rect.collidesWith(m_Graphic.rect_Gameplay[1]) && state == MC_DOWN)
		{
			switch_State(MC_IDLE);
			return true;
		}

		return false;
	}
	int total=0;
	public void shoot()
	{
//		int inScreen = totalInScreen();
//		for (int i = 0; i < listPanahRama.length; i++)
//		{	
		listPanahRama[total].rectPanah.setPosition(m_Mc.getX(), m_Mc.getY()+Utils.getRatioH(40));	
		listPanahRama[total].sprPanah.setPosition(m_Mc.getX(), m_Mc.getY()+Utils.getRatioH(40));
		listPanahRama[total].panahApiAnim.setPosition(m_Mc.getX(), m_Mc.getY()+Utils.getRatioH(40));
			Utils.TRACE("tembak....");
//		}
		
		if (total < listPanahRama.length - 1)
		{
			total++;
		}
		else {
			total = 0;
		}
	}
	
	int totalInScreen()
	{
		int total=0;
		for (int i = 0; i < listPanahRama.length; i++)
		{
			if(listPanahRama[i].rectPanah.getX() < RES_WIDTH && listPanahRama[i].rectPanah.getY() < RES_HEIGHT && listPanahRama[i].rectPanah.getY() > 0){
				total += 1;
			}
		}
		return total;
		
	}

	public int getCurRamaState()
	{
		return m_Mc_State;
	}

	public float getPosX()
	{
		return m_Mc.getX();
	}

	public float getPosY()
	{
		return m_Mc.getY();
	}
}
